package com.dotools.privacy;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dotools.procommon.R;
import com.dotools.umlibrary.UMPostUtils;
import com.dotools.utils.UiUtils;


public class AgreementCheckDialog implements UnJoinDialog.UnJoin {
    private AlertDialog mDialog = null;
    private OnBtnClickListener mClickListener = null;
    private AlertDialog.Builder builder;
    private View view;

    @Override
    public void onOkClick() {
        if (mClickListener != null) {
            mClickListener.onOkClick();
        }
    }

    @Override
    public void onUnOKClick() {
        if (mClickListener != null) {
            mClickListener.onUnOKClick();
        }
    }

    public interface OnBtnClickListener {
        void onOkClick();

        void onUnOKClick();
    }

    public AgreementCheckDialog(final Context context) {
        init(context,null);
    }


    public AgreementCheckDialog(final Context context, String content) {
        init(context,content);
    }


    private void init(final Context context, String content) {
        builder = new AlertDialog.Builder(context, R.style.Privacy_Dialog_Style);
        view = LayoutInflater.from(context).inflate(R.layout.privacy_check_dialog, null, false);
        LinearLayout body= view.findViewById(R.id.privacy_body);
        TextView ok = view.findViewById(R.id.user_privacy_ok);
        TextView none = view.findViewById(R.id.user_privacy_none);
        TextView msg = view.findViewById(R.id.msg);
        TextView gotText = view.findViewById(R.id.privacy_got_text);
        TextView contentText = view.findViewById(R.id.content);

        CheckBox checkBox = view.findViewById(R.id.privacy_check);

        SpannableString spannableString = new SpannableString("我已阅读");
        SpannableString spannableString1 = new SpannableString("《用户协议》");
        SpannableString spannableString2 = new SpannableString("及");
        SpannableString spannableString3 = new SpannableString("《隐私政策》");
        spannableString1.setSpan(new ClickText(context, true), 1, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString3.setSpan(new ClickText(context, false), 1, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(spannableString);
        spannableStringBuilder.append(spannableString1);
        spannableStringBuilder.append(spannableString2);
        spannableStringBuilder.append(spannableString3);
        String str="欢迎使用本产品，本产品非常重视您的隐私和个人信息。在您使用本产品前，请认真阅读《用户协议》及《隐私政策》，";
        if(content!=null){
            RelativeLayout.LayoutParams  params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, UiUtils.dip2px(context,450f));
            int margin=UiUtils.dip2px(context,20f);
            params.setMargins(margin,margin,margin,margin);
            body.setLayoutParams(params);
            contentText.setText(content);
            str+="另外为了更好地使用产品功能，需征求您的允许，获得以下权限:";
        } else{
            contentText.setVisibility(View.GONE);
            str+="您接受并同意条款后即可开始使用本产品。";
        }
        msg.setText(str);
        gotText.setText(spannableStringBuilder);
        gotText.setMovementMethod(LinkMovementMethod.getInstance());
        gotText.setHighlightColor(Color.WHITE); //设置点击后的颜色为透明
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox.isChecked()){
                    dialogDismiss();
                    if (mClickListener != null) {
                        mClickListener.onOkClick();
                    }
                }else{
                    Toast.makeText(context,"请勾选我已阅读",Toast.LENGTH_SHORT).show();
                }
            }
        });
        none.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogDismiss();
                UMPostUtils.INSTANCE.onEvent(context, "disagree_click");
                UnJoinDialog unJoinDialog = new UnJoinDialog(context);
                unJoinDialog.setOkClick(AgreementCheckDialog.this);
            }
        });

    }


    public void show() {
        mDialog = builder.setView(view).create();
        mDialog.setCanceledOnTouchOutside(false);//点击屏幕不消失
        mDialog.setCancelable(false);
        // 设置参数
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        // 宽度和高度
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        mDialog.getWindow().setAttributes(params);
        mDialog.show();
    }

    public AgreementCheckDialog setOkClick(OnBtnClickListener clickListener) {
        this.mClickListener = clickListener;
        return this;
    }

    public void dialogDismiss() {
        if (mDialog != null) {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
                mDialog = null;
            }
        }
    }
}

